package com.example.cmh.controller;

import com.example.cmh.common.Result;
import com.example.cmh.pojo.Department;
import com.example.cmh.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Map;

/**
 * 部门控制器
 */
@RestController
@RequestMapping("/api/department")
@CrossOrigin
public class DepartmentController {
    
    @Autowired
    private DepartmentService departmentService;
    
    /**
     * 分页条件搜索部门信息
     * @param departmentName 部门名称
     * @param departmentStatus 部门状态
     * @return 分页结果
     */
    @GetMapping("/list")
    public Result getDepartments(@RequestParam(name = "departmentName", required = false) String departmentName,
                                @RequestParam(name = "departmentStatus", required = false) String departmentStatus) {
        Map<String, Object> result = departmentService.getDepartments(departmentName, departmentStatus);
        return Result.success("获取部门列表成功", result);
    }
    
    /**
     * 根据departmentId查找对应的department
     * @param departmentId 部门ID
     * @return 部门信息
     */
    @GetMapping("/{departmentId}")
    public Result getDepartmentsById(@PathVariable(name = "departmentId") Integer departmentId) {
        Department result = departmentService.getDepartmentsById(departmentId);
        return Result.success("获取部门成功", result);
    }
    
    /**
     * 新增部门
     * @param department 部门信息
     * @return 操作结果
     */
    @PostMapping("/add")
    public Result addDepartments(@Valid @RequestBody Department department) {
        departmentService.addDepartments(department);
        return Result.success("新增部门成功");
    }
    
    /**
     * 更新部门信息
     * @param departmentId 部门ID
     * @param department 部门信息
     * @return 操作结果
     */
    @PutMapping("/{departmentId}")
    public Result updateDepartment(@PathVariable(name = "departmentId") Integer departmentId, @Valid @RequestBody Department department) {
        department.setDepartmentId(departmentId);
        departmentService.updateDepartment(department);
        return Result.success("更新成功");
    }
    
    /**
     * 删除部门
     * @param departmentId 部门ID
     * @return 操作结果
     */
    @DeleteMapping("/{departmentId}")
    public Result deleteDepartment(@PathVariable(name = "departmentId") Integer departmentId) {
        departmentService.deleteDepartment(departmentId);
        return Result.success("删除成功");
    }
} 